package com.iaiai.imail.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 
 * <p>
 * Title: DatabaseOpenHelper.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-8-20 下午2:55:52
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class DatabaseOpenHelper extends SQLiteOpenHelper {

	private static final String DBNAME = "iaiai"; // 数据库名子

	private static final int VERSION = 1; // 版本

	private static DatabaseOpenHelper databaseOpenHelper;

	public static SQLiteOpenHelper getInstance(Context context) {
		if (databaseOpenHelper == null) {
			databaseOpenHelper = new DatabaseOpenHelper(context);
		}
		return databaseOpenHelper;
	}

	public static SQLiteOpenHelper getInstance() {
		return databaseOpenHelper;
	}

	public DatabaseOpenHelper(Context context) {
		// 第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类
		super(context, DBNAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// 创建表
		// |--------------------------------------------------
		// |table:ACCOUNT | 帐号
		// |--------------------------------------------------
		// |ID          | INTEGER     |
		// |ACCOUNT     | VARCHAR(50) |email帐号
		// |USERNAME    | VARCHAR(50) |用户名
		// |PASSWORD    | VARCHAR(50) |密码
		// |CREATEDATE  | VARCHAR(20) |创建时间
		// |GETPORT     | INTEGER     |获得邮件端口[默认是pop3的110端口]
		// |SENDPORT    | INTEGER     |发送邮件端口[默认是smtp的25端口]
		// |GETSERVICE  | VARCHAR(50) |接收邮件服务器[如：pop.qq.com]
		// |SENDSERVICE | VARCHAR(50) |发送邮件服务器[如：smtp.qq.com]
		// |UVALIDATE   | INTEGER     |发送邮件身份验证
		// |RCOUNT      | INTEGER     |读取邮件的次数
		// |--------------------------------------------------
		db.execSQL("CREATE TABLE EMAIL(" +
				"ID INTEGER PRIMARY KEY AUTOINCREMENT," +
				"ACCOUNT VARCHAR(50)," +
				"USERNAME VARCHAR(50)," +
				"PASSWORD VARCHAR(50)," +
				"CREATEDATE VARCHAR(20)," +
				"GETPORT INTEGER," +
				"SENDPORT INTEGER," +
				"GETSERVICE VARCHAR(50)," +
				"SENDSERVICE VARCHAR(50)," +
				"UVALIDATE INTEGER," +
				"RCOUNT INTEGER)");
		// 创建表
		// |--------------------------------------------------
		// |table:INBOX | 收件箱
		// |--------------------------------------------------
		// |ID          | INTEGER      |
		// |EID         | INTEGER      |email帐号id
		// |TITLE       | TEXT         |标题
		// |CONTENT     | TEXT         |内容
		// |FORM        | TEXT         |来自
		// |TOADDRESS   | TEXT         |发送到
		// |CC          | TEXT         |抄送人
		// |BCC         | TEXT         |密送人
		// |SEND_DATE   | VARCHAR(50)  |发送时间
		// |REPLYSIGN   | INTEGER      |要求回执[0:否/1:是]
		// |ATTACH      | INTEGER      |包含附件[0:否/1:是]
		// |CONTENTTYPE | VARCHAR(50)  |邮件类型[text/plain]-[text/html]-[multipart/*]
		// |DISPOSITION | TEXT         |
		// |DESCRIPTION | TEXT         |描述
		// |SIZE        | INTEGER      |尺寸
		// |HEADER      | TEXT         |邮件头信息
		// |CREATE_DATE | VARCHAR(50)  |接收时间
		// |READ        | INTEGER      |是否查看[0:否/1:是]
		// |--------------------------------------------------
		db.execSQL("CREATE TABLE INBOX(" +
				"ID INTEGER PRIMARY KEY AUTOINCREMENT," +
				"EID INTEGER," +
				"MID VARCHAR(100),"+
				"TITLE TEXT," +
				"CONTENT TEXT," +
				"FORM TEXT," +
				"TOADDRESS TEXT," +
				"CC TEXT," +
				"BCC TEXT," +
				"SEND_DATE VARCHAR(50)," +
				"REPLYSIGN INTEGER," +
				"ATTACH INTEGER," +
				"CONTENTTYPE VARCHAR(50)," +
				"DISPOSITION TEXT," +
				"DESCRIPTION TEXT," +
				"SIZE INTEGER," +
				"HEADER TEXT," +
				"CREATE_DATE VARCHAR(50)," +
				"READ INTEGER)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// 升级更新
		db.execSQL("DROP TABLE IF EXISTS EMAIL");
		db.execSQL("DROP TABLE IF EXISTS INBOX");
		onCreate(db);
	}

}
